package Juego.control;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

import com.thoughtworks.xstream.io.path.Path;

public class controlMapa {
	ArrayList< String > Names;
	Path P = new Path("src/Mapas/");
	public controlMapa() {
		initIndex();
	}
	
	public String[] getMapa(){
		String [] ans = null;
		printIndex();
		Scanner In = new Scanner( System.in );		
		return readMap( In.nextInt() - 1 );
	}
	
	private String[] readMap(int id ) {
		String [] ans = null;
		FileReader Fr = null ;
		try {
			Fr = new FileReader( P.toString() + Names.get( id ) );
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedReader Read = new BufferedReader( Fr );
		StringTokenizer St = null;
		try {
			St = new StringTokenizer( Read.readLine() );
			int X , Y;
			X = Integer.parseInt( St.nextToken() );
			Y = Integer.parseInt( St.nextToken() );
			ans = new String[ X ];
			for( int i = 0 ; i < X ; i++ ){
				ans[ i ] = Read.readLine();				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ans;
	}

	void printIndex(){
		int i = 1;
		System.out.println("Seleccione el mapa:");
		for( String S : Names ){
			System.out.println(i+". " + S );
			i++;
		}
	}
	
	public void initIndex(){
		FileReader File = null;
		ArrayList< String > Nombres = new ArrayList<String>();
		try {
			
			File = new FileReader( P.toString()+"index.txt" ); // TODO configurar en windows != que en linux
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedReader In = new BufferedReader( File );
		StringTokenizer Tokenizer = null ;
		try {
			Tokenizer = new StringTokenizer( In.readLine() );
			//System.out.println( Tokenizer.nextToken() );
			int sz = Integer.parseInt( Tokenizer.nextToken() );
			for( int i = 0 ; i < sz ; i++ ){
				Nombres.add( In.readLine() );
				System.out.println( Nombres.get( i ) );
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Names = Nombres;
	}
}
